package com.hyt.it.ogt.kq.service.gov.model.excel.listener;

import com.alibaba.excel.event.AnalysisEventListener;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author 刘恒活
 * @since 2021/8/27 14:20
 */
public abstract class AbstractAnalysisListener<T> extends AnalysisEventListener<T> {
    private final List<List<String>> list = new ArrayList<>();
    protected void addData(Map<Integer, String> data){
        List<String> row = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            row.add(data.get(i));
        }
        list.add(row);
    }

    protected void checkHeadField(Map<Integer, String> headMap, List<String> standardHeadMap, Logger log) {
        if (MapUtils.isNotEmpty(headMap)) {
            Collection<String> headVal = headMap.values();
            StringBuilder diffBuilder = new StringBuilder().append("字段缺失");
            //差别字段总数
            int diffSize = 0;
            //以模版表头为准
            for (String index : standardHeadMap) {
                if (!headVal.contains(index)) {
                    diffBuilder.append(" ").append(index);
                    diffSize++;
                }
            }
            if (diffSize > 0) {
                log.error(diffBuilder.toString());
                throw new KqException(0, diffBuilder.toString());
            }
        }
    }

    public List<List<String>> getList() {
        return list;
    }


}
